"""
Basic mission loading and mission utilities module
"""
import pygame
import math
import ai
from random import *
import game

class Mission:
    def __init__(self):
        pass
    
    def tick(self):
        pass
    
    def victory_condition(self):
        pass
    
    def defeat_condition(self):
        pass
    
    def draw(self, viewport):
        pass

class Test(Mission):
    max_text_ticks = 30
    def __init__(self, player_class, foe_class, initial_friends = 5):
        Mission.__init__(self)
        game.ships.empty()
        game.bullets.empty()
        self.team1 = pygame.sprite.Group()
        self.team2 = pygame.sprite.Group()
        self.team1_class = player_class
        self.team2_class = foe_class
        
        self.max_friends = initial_friends
        
        self.display_text = ''
        self.text_ticks = 0
        
        self.player = player_class()
        game.cam.add(self.player)
        game.ships.add(self.player)
        self.team1.add(self.player)
        for i in xrange(initial_friends):
            e = player_class(ai.Simple)
            e.position = [randint(-200, 200), randint(-200, 200)]
            game.ships.add(e)
            self.team1.add(e)
        
        heading = randint(0, 359)
        speed = random() * 30
        sy = math.sin(math.radians(heading)) * speed
        sx = math.cos(math.radians(heading)) * speed
        self.waves = -1
        for s in self.team1:
            s.heading = heading
            s.speed = [sx + random(), sy + random()]
        
        self.next_wave_due = pygame.time.get_ticks() + 10000
    
    def draw(self, viewport):
        #cx = 0
        #cy = 0
        #for s in self.team1: 
        #    cx += s.rect.centerx
        #    cy += s.rect.centery
        #cx /= len(self.team1)
        #cy /= len(self.team1)
        #game.font.render('o', (cx, cy))
        
        
        
        if self.player.health < 0:
            s = 'You survived ' + str(self.waves)\
                + ' waves\nPress "r" to restart'
            game.font.render(s,
                             (game.dimensions.w / 2 - game.font.length(s, 3) / 2,
                              game.dimensions.h / 2), zoom=3)
            return
        elif self.text_ticks:
            tint = float(self.text_ticks) / self.max_text_ticks
            z = 3 - tint
            x = game.dimensions.w / 2 - game.font.length(self.display_text, z) / 2
            game.font.render(self.display_text,
                             (x, game.dimensions.h / 2),
                             tint=(0, 1, 0, tint),
                             zoom=z)
            self.text_ticks -= 1
        else:
            game.font.render(str(int(self.next_wave_due -
                             pygame.time.get_ticks()) / 1000) +
                             ' untill next wave', (game.dimensions.w - 200,
                             game.dimensions.h - 250))
    
    def tick(self):
        if self.player.health < 0:
            return
        if(game.radar_color[0] > 0):
            game.radar_color = game.radar_color[0] * 0.95, 0, 1, 1
        
        if (pygame.time.get_ticks() > self.next_wave_due):
            self.waves += 1
            
            #update radar
            game.radar_color = (1, 0, 0, 1)
            
            friends = len(self.team1)
            foes = len(self.team2)
            new = (self.waves + 1) + randint(0, friends/2)
            pos = list(self.player.position)
            r = (random() * math.pi * 2)
            d = randint(3000, 5000)
            pos[0] += math.cos(r) * d
            pos[1] += math.sin(r) * d
            self.display_text = str(new) + ' boogies inbound!'
            self.text_ticks = self.max_text_ticks
            for i in xrange(0, new):
                e = self.team2_class(ai.Simple)
                game.ships.add(e)
                self.team2.add(e)
                e.position = [pos[0] + randint(-200, 200),
                              pos[1] + randint(-200, 200)]
            
            if friends == 1 or not randint(0, max(0, 6 - self.waves)):
                self.display_text += '\nReinforcements!'
                self.text_ticks = self.max_text_ticks
                for s in self.team1:
                    s.health = s.MAX_HEALTH
                    for i in xrange(self.max_friends - friends):
                        e = self.team1_class(ai.Simple)
                        e.position = [randint(-200, 200), randint(-200, 200)]
                        game.ships.add(e)
                        self.team1.add(e)
            
            self.next_wave_due = pygame.time.get_ticks() + (5000 +\
                                 (10000 * random())) + (5000 * (foes + 1))
